package com.pray.praymall.cart.controller;

import com.pray.praymall.cart.interceptor.CartInterceptor;
import com.pray.praymall.cart.service.CartService;
import com.pray.praymall.cart.vo.CartItemVo;
import com.pray.praymall.cart.vo.CartVo;
import com.pray.praymall.cart.vo.UserInfoTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * do something
 *
 * @author 魂悸魄
 * @date 2020/9/23 22:06
 */
@Controller
public class CartController {

    @Autowired
    private CartService cartService;

    @GetMapping("/currentUserCartItems")
    @ResponseBody
    public List<CartItemVo> getCurrentUserCartItems() {
        return cartService.getUserCartItems();
    }

    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId") Long skuId) {
        cartService.deleteItem(skuId);
        return "redirect:http://cart.praymall.com/cart.html";
    }

    @GetMapping("/countItem")
    public String countItem(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num) {
        cartService.changeItemCount(skuId, num);
        return "redirect:http://cart.praymall.com/cart.html";
    }

    /**
     * 选中给定的购物项
     * @param skuId 商品id
     * @param check 选中状态，1：选中，0：不选中
     */
    @GetMapping("/checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId, @RequestParam("check") Integer check) {
        cartService.checkItem(skuId, check);
        return "redirect:http://cart.praymall.com/cart.html";
    }

    /**
     * 浏览器有一个cookie：user-key --> 用来标识用户身份，一个月后过期
     * 如果第一次使用jd的购物车功能，都会给一个临时的用户身份
     * 浏览器保存以后，每次访问都会带上这个cookie
     * 登录：session有
     * 没登录：按照cookie里面带来的user-key来处理
     * 第一次：如果没有临时用户，帮忙创建一个临时用户
     * @return view
     */
    @GetMapping("/cart.html")
    public String cartListPage(Model model) throws ExecutionException, InterruptedException {
        // 快速得到用户信息
        CartVo cartVo = cartService.getCart();
        model.addAttribute("cart", cartVo);
        return "cartList";
    }

    /**
     * 添加商品到购物车
     * RedirectAttributes#addFlashAttribute()：将数据放在session里面，可以在页面取出，但是只能取一次
     * RedirectAttributes#addAttribute()：将数据放在url后面
     * @return 重定向到addToCartSuccess.html
     */
    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num, RedirectAttributes ra) throws ExecutionException, InterruptedException {
        cartService.addToCart(skuId, num);
        ra.addAttribute("skuId", skuId);
        return "redirect:http://cart.praymall.com/addToCartSuccess.html";
    }

    /**
     * 跳转到成功页
     * @param skuId skuId
     * @param model model
     * @return success.html
     */
    @GetMapping("/addToCartSuccess.html")
    public String addToCartSuccess(@RequestParam("skuId") Long skuId, Model model) {
        CartItemVo cartItemVo = cartService.getCartItem(skuId);
        model.addAttribute("item", cartItemVo);
        return "success";
    }
}
